﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TitlesPage.xaml.cs" company="saramgsilva">
//   Copyright (c) 2012 saramgsilva. All rights reserved.
// </copyright>
// <summary>
//   Defines the TitlesPage type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Netflix.ClientApp.View
{
    using System.Windows.Controls;
    using System.Windows.Navigation;

    using Netflix.PortableClassLibrary.Model;
    using Netflix.PortableClassLibrary.ViewModel;

    /// <summary>
    /// Defines the Titles Page
    /// </summary>
    public partial class TitlesPage
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TitlesPage" /> class.
        /// </summary>
        public TitlesPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Called when a page becomes the active page in a frame.
        /// </summary>
        /// <param name="e">An object that contains the event data.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var titlesViewModel = DataContext as TitlesViewModel;
            if (titlesViewModel != null && (e.NavigationMode == NavigationMode.New || e.NavigationMode == NavigationMode.Refresh))
            {
                await titlesViewModel.LoadDataAsync();
            }
        }

        /// <summary>
        /// Handles the OnSelectionChanged event of the NewTitle control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SelectionChangedEventArgs" /> instance containing the event data.</param>
        private void NewTitle_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var titlesViewModel = DataContext as TitlesViewModel;

            if (titlesViewModel != null)
            {
                var item = lbxTopTitles.SelectedItem as MyTitleItemView;
                if (item != null)
                {
                    titlesViewModel.ShowTitleDetails(item.Item);
                }
            }
        }
    }
}